﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mighty.Common;

namespace Mighty.Brain
{

    class information
    {
        
        public Shape Giruda = new Shape();
        public bool[] UsefulGiruda = new bool[13];
        public int EnemyGiruda = new int();
    }

    public class CardPart
    {
        public List<HandedCard> items = new List<HandedCard>();
        
    }

    public class HanswBrain : BasicBrain
    {
        int GirudaLine;
        const int myFull = 6;

        public CardPart[] cardPart = new CardPart[7];
        information cardInfo = new information();

        // 배팅 : 그림 2점 일반 1점으로 7점이상시 배팅
        public override Betting BettingTicEvent()
        {
            for (int i = 0; i < 7; i++) cardPart[i] = new CardPart();
            int[] card_num = new int[4];
            Betting betting = new Betting();
            
            betting.isPass = true;

            for (int i = 0; i < 4; i++)
                card_num[i] = 0;

            foreach (Card my in gameInfo.myCards)
            {
                // 무늬별 카드 장수 확인
                switch (my.shape)
                {
                    case Shape.Spade:
                        if (my.number > 10)
                            card_num[0] += 2;
                        else 
                            card_num[0] += 1;
                        break;
                    case Shape.Diamond:
                        if (my.number > 10)
                            card_num[1] += 2;
                        else
                            card_num[1] += 1;
                        break;
                    case Shape.Heart:
                        if (my.number > 10)
                            card_num[2] += 2;
                        else
                            card_num[2] += 1;
                        break;
                    case Shape.Clover:
                        if (my.number > 10)
                            card_num[3] += 2;
                        else
                            card_num[3] += 1;
                        break;
                    default:
                        break;
                }
            }

            //제일 장수가 많은것 선택
            int top = 0;
            Giruda top_s = Giruda.Spade;
     
            if (card_num[top] < card_num[1]){
                top_s = Giruda.Diamond;
                top = 1;
                }
            if (card_num[top] < card_num[2])
            {
                top_s = Giruda.Heart;
                top = 2;
            }
            if (card_num[top] < card_num[3])
            {
                top_s = Giruda.Clover;
                top = 3;
            }
            
            if (gameInfo.betting.limit > 0)
            {
                Betting previousBetting = gameInfo.betting;
                if (previousBetting.limit < 15 && card_num[top]>6)
                {
                    betting.isPass = false;
                    betting.giruda = top_s;
                    betting.limit = previousBetting.limit + 1;
                }
            }
            else
            {
                betting.isPass = false;
                betting.giruda = top_s;
                betting.limit = 13;
            }
            if (betting.isPass == true)
                CardListing();
            return betting;
        }

        // 카드선택
        public override HandedCard[] ChoiceCardEvent()
        {
            // 기루다빼고 대충 삭제
            int card = 13;
            for (int i = 0; card>10 && i < 13; i++)
            {
                if (gameInfo.myCards[i].shape != (Shape)gameInfo.betting.giruda)
                {
                    gameInfo.myCards[i].cmdRemove = true;
                    card--;
                }
            }

            return gameInfo.myCards;
        }

        // 마이티 프랜드
        public override FriendPlayer ChoiceFriendEvent()
        {
            bool myghtyCard = false;
            bool jokerCard = false;
            for (int i = 0; i < gameInfo.myCards.Length; i++)
            {
                if (gameInfo.myCards[i].shape == gameInfo.mighty.shape && gameInfo.myCards[i].number == gameInfo.mighty.number)
                    myghtyCard = true;
                else if (gameInfo.myCards[i].shape == Shape.Joker)
                    jokerCard = true;
            }
            gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
            if (!myghtyCard)
            {
                gameInfo.friend.cardCondition = gameInfo.mighty;
            }
            else if (!jokerCard)
            {
                gameInfo.friend.cardCondition = new Card(Shape.Joker, 0);
            }
            else
            {
                gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;
            }
            return gameInfo.friend;
        }

        public void CardListing()
        {
            cardInfo.Giruda=(Shape)gameInfo.betting.giruda;
            cardInfo.EnemyGiruda = 13;
            GirudaLine = (int)gameInfo.betting.giruda;
            for (int i = 0; i < gameInfo.myCards.Length; i++)
            {
                
                if (gameInfo.myCards[i].cmdRemove != true)
                {
                    cardPart[myFull].items.Add(gameInfo.myCards[i]);

                    if (gameInfo.myCards[i].shape == Shape.Spade)
                        cardPart[(int)Shape.Spade].items.Add(gameInfo.myCards[i]);
                    else if (gameInfo.myCards[i].shape == Shape.Clover)
                        cardPart[(int)Shape.Clover].items.Add(gameInfo.myCards[i]);
                    else if (gameInfo.myCards[i].shape == Shape.Heart)
                        cardPart[(int)Shape.Heart].items.Add(gameInfo.myCards[i]);
                    else if (gameInfo.myCards[i].shape == Shape.Diamond)
                        cardPart[(int)Shape.Diamond].items.Add(gameInfo.myCards[i]);
                    else cardPart[(int)Shape.Joker].items.Add(gameInfo.myCards[i]);
                }

                if (gameInfo.myCards[i].shape == cardInfo.Giruda)
                {
                    cardInfo.EnemyGiruda--;
                }

            }
            for (int i = 0; i < 13; i++)
            {
                cardInfo.UsefulGiruda[i] = true;
            }

        }
        public override Betting LastBettingEvent()
        {
            CardListing();
            return gameInfo.betting;
        }
        
        private HandedCard attackRound(List<HandedCard> possibleCards)
        {
            if (gameInfo.roundIdx == 0)
            {
                int max=0;
                for (int i = 1; i < possibleCards.Count(); i++)
                    if (possibleCards[max].number < possibleCards[i].number)
                        max = i;
                return possibleCards[max];
            }
            else
            {
                if (gameInfo.roundCardIdx == 0)
                {
                    if (cardPart[GirudaLine].items.Count > 0 && cardInfo.EnemyGiruda > 0)
                    {
                        int temp = 0;
                        for (int i = 1; i < cardPart[GirudaLine].items.Count; i++)
                        {
                            if (cardPart[GirudaLine].items[temp].number < cardPart[GirudaLine].items[i].number)
                                temp = i;
                        }
                        HandedCard reCard = cardPart[GirudaLine].items[temp];
                        cardPart[GirudaLine].items.RemoveAt(temp);
                        return reCard;
                    }
                    else
                    {
                        return rndCard(possibleCards);
                    }
                }
                else
                {
                    return rndCard(possibleCards);
                }
            }
        }

        private HandedCard friendRound(List<HandedCard> possibleCards)
        {
            int temp = 0;
            for (int i = 1; i < possibleCards.Count; i++)
            {
                if (possibleCards[i].shape == cardInfo.Giruda || possibleCards[temp].number < possibleCards[i].number)
                    temp = i;
            }
            return rndCard(possibleCards, temp);
        }

        private HandedCard etcRound(List<HandedCard> possibleCards)
        {
            int temp=0;
            for (int i = 1; i < possibleCards.Count; i++)
            {
                if (possibleCards[temp].number < possibleCards[i].number)
                    temp = i;
            }
            return rndCard(possibleCards, temp);
        }

        public HandedCard rndCard(List<HandedCard> possibleCards)
        {
            Random r = new Random();
            int k = r.Next(possibleCards.Count);
            
            return possibleCards[k];

        }
        public HandedCard rndCard(List<HandedCard> possibleCards, int i)
        {
            int k;
            Random r = new Random();
            if(r.Next(10)>7)
                k = r.Next(possibleCards.Count);
            else
                k = i;

            return possibleCards[k];

        }


        public override HandedCard RoundTicEvent()
        {

            List<HandedCard> possibleCards = new List<HandedCard>();
            foreach (HandedCard card in gameInfo.myCards)
            {
                if (card.isPossible) possibleCards.Add(card);
            }
            
            if(gameInfo.attackPlayer== gameInfo.myInfo){
                return attackRound(possibleCards);
            }
            else if (gameInfo.friend == gameInfo.myInfo)
            {
                return friendRound(possibleCards);
            }
            else
            {
                return etcRound(possibleCards);
            }
            
            //return rndCard(possibleCards);
        }
    }
}
